package com.example.campuslink.ui.activity.message

import android.widget.ArrayAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.campuslink.BR
import com.example.campuslink.R
import com.example.campuslink.commen.adpter.MessageShowItemAdapter
import com.example.campuslink.commen.enums.MessageTitleEnum
import com.example.campuslink.commen.tools.SpacesItemButtonDecoration
import com.example.campuslink.databinding.ActivityNewAddAttentionBinding
import com.example.campuslink.ui.activity.BaseActivity
import com.example.campuslink.viewModel.MessageViewModel

class NewAddAttentionActivity : BaseActivity<ActivityNewAddAttentionBinding, MessageViewModel>(
    MessageViewModel::class.java,
    R.layout.activity_new_add_attention,
    BR.viewModel
) {
    private var adapter: MessageShowItemAdapter? = null
    private val titleList: List<String> = MessageTitleEnum.getAttentionMessageTitle()
    override fun initView() {
        initData()
        mViewDataBinding?.rvMessage?.adapter = adapter
        mViewDataBinding?.rvMessage?.layoutManager = LinearLayoutManager(this)
        mViewDataBinding?.rvMessage?.addItemDecoration(SpacesItemButtonDecoration(20))
        mViewModel?.messageList?.observe(this) {
            if (it.isNotEmpty()) {
                adapter?.setListData(it)
            }
        }

        mViewDataBinding?.frashLayout?.setOnRefreshListener {
            mViewModel?.getAttentionMessage()
            mViewDataBinding?.frashLayout?.isRefreshing = false
        }
    }

    private fun initData() {
        mViewModel?.context = this
        mViewModel?.initDB(this)
        mViewModel?.getAttentionMessage()
        adapter = MessageShowItemAdapter(mViewModel!!)
        mViewModel?.setMessageType(titleList[0])
        val spinnerAdapter =
            ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, titleList)
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        mViewDataBinding?.spinnerShowTitle?.setAdapter(spinnerAdapter)
    }
}